# Contributor: Timo Teras <timo.teras@iki.fi>
# Maintainer: Timo Teras <timo.teras@iki.fi>

_gccver=6.4.0
_gccpkgrel=12

pkgname=java-gcj-compat
pkgver=$_gccver
_mypkgrel=5
pkgrel=$(( $_mypkgrel + $_gccpkgrel ))
pkgdesc="JDK like environment using GCJ"
url="https://git.alpinelinux.org/aports.git"
# riscv64 blocked by gcc6
arch="all !riscv64"
license="GPL-2.0-or-later"
depends="fastjar gcc6-java=$_gccver-r$_gccpkgrel java-common"
makedepends="gcc6-java=$_gccver-r$_gccpkgrel zlib-dev"

prepare() {
	(
	gccver=$(. "$startdir"/../../community/gcc6/APKBUILD ; echo "$pkgver")
	gccpkgrel=$(. "$startdir"/../../community/gcc6/APKBUILD ; echo "$pkgrel")
	if [ "$gccver" != "$_gccver" ] || [ "$gccpkgrel" != "$_gccpkgrel" ]; then
		die "gcc6 version ($gccver) needs to match _gccver ($_gccver)"
	fi
	)
	mkdir -p $builddir
}


build() {
	gcj-6 -Wl,-Bsymbolic -findirect-dispatch -o ecj \
		--main=org.eclipse.jdt.internal.compiler.batch.Main \
		/usr/share/java/ecj.jar -lgcj
}

check() {
	$builddir/ecj > /dev/null
}

package() {
	JVM_DIR=$pkgdir/usr/lib/jvm
	JDK_DIR=$JVM_DIR/java-1.5-gcj
	JDK_BIN_DIR=$JDK_DIR/bin
	JDK_LIB_DIR=$JDK_DIR/lib
	JRE_DIR=$JDK_DIR/jre
	JRE_BIN_DIR=$JRE_DIR/bin
	JRE_LIB_DIR=$JRE_DIR/lib

	CPU=$(uname -m | sed -e 's/i.86/i386/g' -e 's/x86_64/amd64/g')

	mkdir -p $JDK_BIN_DIR $JDK_LIB_DIR
	ln -sf ../../../../bin/gij-6 $JDK_BIN_DIR/java
	ln -sf ../../../../bin/fastjar $JDK_BIN_DIR/fastjar
	ln -sf ../../../../bin/sinjdoc $JDK_BIN_DIR/javadoc
	ln -sf ../../../../bin/grmic-6 $JDK_BIN_DIR/rmic
	ln -sf ../../../../bin/gjavah-6 $JDK_BIN_DIR/javah
	install -s $builddir/ecj $JDK_BIN_DIR/javac
	ln -sf ../../../../bin/gappletviewer-6 $JDK_BIN_DIR/appletviewer
	ln -sf ../../../../bin/gjarsigner-6 $JDK_BIN_DIR/jarsigner
	ln -sf ../../../../bin/grmiregistry-6 $JDK_BIN_DIR/rmiregistry
	ln -sf ../../../../bin/gkeytool-6 $JDK_BIN_DIR/keytool
	ln -sf ../../../../bin/gjar-6 $JDK_BIN_DIR/gjar
	ln -sf ../../../../bin/gnative2ascii-6 $JDK_BIN_DIR/gnative2ascii
	ln -sf ../../../../share/java/libgcj-tools-$_gccver.jar $JDK_LIB_DIR/tools.jar

	gcjrel=$(ls -d /usr/lib/gcj-$_gccver-* | cut -d '-' -f 3)

	mkdir -p $JRE_BIN_DIR $JRE_LIB_DIR/$CPU
	ln -sf ../../../../../bin/gij-6 $JRE_BIN_DIR/java
	ln -sf ../../../../../bin/grmiregistry-6 $JRE_BIN_DIR/rmiregistry
	ln -sf ../../../../../bin/gkeytool-6 $JRE_BIN_DIR/keytool
	ln -sf ../../../../../share/java/libgcj-$_gccver.jar $JRE_LIB_DIR/rt.jar
	ln -sf ../../../../../../lib/gcj-$_gccver-$gcjrel/libjvm.so $JRE_LIB_DIR/$CPU
	ln -sf ../../../../../../lib/gcj-$_gccver-$gcjrel/libjavamath.so $JRE_LIB_DIR/$CPU
	ln -sf ../../../../../../lib/gcj-$_gccver-$gcjrel/classmap.db $JRE_LIB_DIR/$CPU
}

